package com.fywsy.fyweb.repository;

import com.fywsy.fyweb.entity.Cdk;
import com.fywsy.fyweb.entity.Qufu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CdkRepository extends JpaRepository<Cdk,Long> {

    Cdk findFirstByCode(String code);

    /**
     * 取出最近激活的那一条
     * @param gameId
     * @param uid
     * @return
     */
    Cdk findFirstByGameIdAndUidOrderByUseTimeDesc(long gameId, String uid);
    Cdk findFirstByGameIdAndUidAndBanOrderByUseTimeDesc(long gameId, String uid,boolean ban);

    Cdk findFirstByExtractAndBan(String extract, boolean ban);

    @Query(value = "SELECT * FROM cdk c WHERE c.game_id IN :gameIds AND c.uid IN :uids AND c.use_time = (SELECT MAX(c2.use_time) FROM cdk c2 WHERE c2.game_id = c.game_id AND c2.uid = c.uid)", nativeQuery = true)
    List<Cdk> findLatestByGameIdsAndUids(@Param("gameIds") List<Long> gameIds, @Param("uids") List<String> uids);

}